package com.abigdreamer.swing.app;

import com.abigdreamer.swing.EmptyComponent;
import com.abigdreamer.swing.component.JCLabel;
import craky.component.JImagePane;
import craky.layout.LineLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.List;

public class Form
  extends JImagePane
{
  private static final long serialVersionUID = 4490673353232645860L;
  
  public Form()
  {
    setLayout(new LineLayout(8, 15, 15, 15, 0, 10, 10, 1));
    setBackground(new Color(255, 255, 255, 210));
    
    List<Component> items = initFormItems();
    if (items != null) {
      setFormItems(items);
    }
  }
  
  public void setFormItems(List<? extends Component> formItems)
  {
    for (Object object : formItems) {
      if (!(object instanceof FormItem))
      {
        add((Component)object, "StartFill");
      }
      else
      {
        FormItem formItem = (FormItem)object;
        
        EmptyComponent item = new EmptyComponent();
        item.setLayout(new BorderLayout());
        item.setPreferredSize(getItemPreferredSize());
        
        JCLabel label = new JCLabel(formItem.label);
        label.setPreferredSize(new Dimension(60, -1));
        
        item.add(label, "West");
        item.add(formItem.item, "Center");
        item.setVisible(formItem.visible);
        
        add(item, "StartFill");
      }
    }
  }
  
  protected List<Component> initFormItems()
  {
    return null;
  }
  
  protected Dimension getItemPreferredSize()
  {
    return new Dimension(-1, 22);
  }
  
  public static class FormItem
    extends Component
  {
    private static final long serialVersionUID = 8488445703014483813L;
    String label;
    Component item;
    boolean visible;
    
    public FormItem(String label, Component item)
    {
      this.label = label;
      this.item = item;
      this.visible = true;
    }
    
    public FormItem(String label, Component item, boolean visible)
    {
      this.label = label;
      this.item = item;
      this.visible = visible;
    }
  }
}
